﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.UI;
using System.Web.UI.WebControls;
using System.Web.Services;
using System.Web.Script.Services;
using MLM.Data;
using System.Web.Script.Serialization;

namespace MLM.AdminCP.Pages
{
    public partial class TopListmissing : System.Web.UI.Page
    {
        protected void Page_Load(object sender, EventArgs e)
        {

        }
        [WebMethod]
        [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
        public static string GetData()
        {
           
            List<listObject> _listObject = new List<listObject>();

            List<DateTime> listDate = new List<DateTime>();
            List<int> _listValue = new List<int>();

            List<ListTopProduct> _listTopProduct = new List<ListTopProduct>();
            List<string> _listProductName = new List<string>();

            List<InvoiceDetail> _temp = new List<InvoiceDetail>();
          

            using (var ct = new MLMDataContext())
            {

                var listOfOrdersByDay =
                    ct.InvoiceDetails.Where(
                        it => it.Invoice.Status == 1 && it.Invoice.InvoiceID == it.InvoiceID).Select(p => p.ProductBaseID).Distinct();//&& it.DateTimeCreated >= dateStart && it.DateTimeCreated <= dateEnd


                foreach (var item2 in listOfOrdersByDay)
                {

                    long? value = 0;
                    var x = ct.InvoiceDetails.Where(p => p.ProductBaseID == item2);
                    foreach (var z in x.ToList())
                    {
                        value += z.Quantity;
                    }
                    _listValue.Add(int.Parse(value.ToString()));
                    _listProductName.Add(x.FirstOrDefault().ProductBas.Name);

                }

            }
            //}
            _listTopProduct.Add(new ListTopProduct { ProductName = _listProductName, CountProduct = _listValue });
            JavaScriptSerializer ser = new JavaScriptSerializer();
            return ser.Serialize(_listTopProduct);
        }
    }
    public class ListTopProduct
    {
        public List<string> ProductName { get; set; }
        public List<int> CountProduct { get; set; }
    }
}